﻿using POC.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace wlog.Shell.IO
{
    public class LsCommand : Command
    {
        public override void Setup(string[] args)
        {
            CommandArgs = args;
            base.TokenizeCommand(args);
            Results.Add("Command Name : " + CommnadName);
            base.CreateTask(() =>
            {
                DirectoryInfo root = new DirectoryInfo(Program.CurrentDirectory);               
                string iocommand = commandVerb.ToLower();
                string folderPath = commandObjectType.ToLower();
                string tempDir = Path.Combine(Program.CurrentDirectory, folderPath);
                bool targetExists = Directory.Exists(tempDir);
                switch(iocommand)
                {
                    case "dir":
                    case "ls":
                        ShowDirectory(root);
                        break;
                    case "cd":
                        switch(folderPath)
                        {

                            case ReserveredFolderNames.BackToRoot:
                                if (targetExists)
                                {
                                    ChangeDirectory(tempDir);
                                }
                                else
                                {
                                    string targetDirectory = root.Root.Name;
                                    ChangeDirectory(targetDirectory);
                                }
                                break;
                            case ReserveredFolderNames.OneLevelUp:
                                if (targetExists)
                                {
                                    ChangeDirectory(tempDir);
                                }
                                else
                                {
                                    string targetDirectory = root.Parent.FullName;
                                    ChangeDirectory(targetDirectory);
                                }
                                break;
                            default:
                                if (targetExists)
                                {
                                    ChangeDirectory(tempDir);
                                }
                            break;
                        }
                        break;                  
                    default:
                        ShowDirectory(root);
                        break;
                }
            });
        }
        public void ChangeDirectory(string fullPath)
        {
            Program.CurrentDirectory = fullPath;
        }
        public void ShowDirectory(DirectoryInfo root)
        {
            foreach (DirectoryInfo dir in root.GetDirectories())
            {
                Results.WriteKeyPair(dir.Name, "[dir]", 40);
            }
            foreach (FileInfo file in root.GetFiles())
            {
                Results.WriteKeyPair(file.Name, file.Length, 40);
            }
        }
    }
}
